package com.lv.tools.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.lv.xulin.demopro.R;

public class PorterDuffView extends ImageView {

    //背景图气泡位置
    private String showOrientation="right";

    public PorterDuffView(Context context) {
        super(context);
    }

    public PorterDuffView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PorterDuffView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PorterDuffView);
        showOrientation=a.getString(0);
        a.recycle();
    }

    public Bitmap getBitmap(Drawable drawable, Context context) {
        Bitmap bitmap_bg = drawableBackgroundBitmap(context);
        Bitmap bitmap_in = drawableToBitmap(drawable.getCurrent());
        final Bitmap bp = getRoundCornerImage(bitmap_bg, bitmap_in);
        return getShardImage(bitmap_bg, bp);
    }

    /**
     * @param bitmap
     * @param context
     * @return
     */
    public Bitmap getBitmap(Bitmap bitmap, Context context) {
        Bitmap bitmap_bg = drawableBackgroundBitmap(context);
        Bitmap bitmap_in =bitmap;
        final Bitmap bp = getRoundCornerImage(bitmap_bg, bitmap_in);
        return getShardImage(bitmap_bg, bp);
    }

    public String getShowOrientation(){
        return showOrientation;
    }

    public  Bitmap drawableBackgroundBitmap(Context context) {

        if(getShowOrientation().contains("right")){
            return BitmapFactory.decodeResource(context.getResources(), R.mipmap.cht_bg_bubble_to);
        }
        return BitmapFactory.decodeResource(context.getResources(), R.mipmap.cht_bg_bubble_from);
    }

    public  Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    public  Bitmap getRoundCornerImage(Bitmap bitmap_bg, Bitmap bitmap_in) {
        Bitmap roundConcerImage = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(roundConcerImage);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, 500, 500);
        Rect rectF = new Rect(0, 0, bitmap_in.getWidth(), bitmap_in.getHeight());
        paint.setAntiAlias(true);
        NinePatch patch = new NinePatch(bitmap_bg, bitmap_bg.getNinePatchChunk(), null);
        patch.draw(canvas, rect);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap_in, rectF, rect, paint);
        return roundConcerImage;
    }

    public  Bitmap getShardImage(Bitmap bitmap_bg, Bitmap bitmap_in) {
        Bitmap roundConcerImage = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(roundConcerImage);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, 500, 500);
        paint.setAntiAlias(true);
        NinePatch patch = new NinePatch(bitmap_bg, bitmap_bg.getNinePatchChunk(), null);
        patch.draw(canvas, rect);
        Rect rect2 = new Rect(0, 0, 500, 500);
        canvas.drawBitmap(bitmap_in, rect, rect2, paint);
        return roundConcerImage;
    }

}
